(function () {
    /*
     * 页面公共参数
     */
    var o = {};

    /*
     * 给页面参数赋值
     */
    function setParam() {
        var result = $.extend(true, o, getUrlParamsToJson());

        return result;
    }

    function initPage() {
        isLogin(function() {
            window.location = 'admin.html';
        }, function() {
            $('#btn_submit').on('click', function () {
                submit();
            });
        });
    }

    /**
     * 提交登陆
     * 
     */
    function submit() {
        var verArr = [
            '#name', '#pwd'
        ];

        var result = Common.verificationControlVal(verArr);

        if (result) {
            return false;
        }

        var data = $('#frm').serializeObject();

        var url = createServerUrl('login');

        Common.AjaxPostData(url, data, function (r) {
            if (r.code == 0) {  //成功
                Common.toast('登录成功', 2);
                var result = r.msg;

                var obj = {
                    id: result.id,
                    name: data.name,
                    token: result.token
                }

                saveJsonData().setData('user', obj, 1);

                setTimeout(function () {
                    window.location = 'admin.html';
                }, 2000);
            } else if (r.code == 1) {   //账号或密码错误
                Common.toast('账号或密码错误', 5000);
            }
        });
    }

    $(function () {
        o = setParam();

        initPage();
    });
})();